package com.domain.cumanager;

import java.util.Hashtable;
import java.util.Iterator;

public class CUStubGarbageCollector extends Thread{
	
	private Hashtable<String, CUStub> m_cuStubs;
	private boolean m_shouldRun = true;
	
	
	public CUStubGarbageCollector(Hashtable<String, CUStub> cuStubs){
		m_cuStubs = cuStubs;

	}
	
	
	
	public void run() {
		while (m_shouldRun) {
			
			garbageCollectorSleep();
			Iterator<String> iter = m_cuStubs.keySet().iterator();
			
			while (iter.hasNext()){
				String curr = iter.next();
				if (m_cuStubs.get(curr).getStatus() == 0){ //"0" means not alive
					m_cuStubs.remove(curr);
				}
			}
		}
	
	}
	
	public void stopWorking(){
		m_shouldRun = false;
	}
	
	private void garbageCollectorSleep() {
		try {
			Thread.sleep(5000);
		} catch (InterruptedException e) {
			//s_logger.fatal("Interrupted while sleeping");  		//TODO: error massage to the logger
		}
	}
}
